package com.devilvan.betrice.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.devilvan.betrice.constant.AdminConstant;
import com.devilvan.betrice.dto.SysConfigDto;
import com.devilvan.betrice.po.SysConfigPo;
import com.devilvan.betrice.service.SysConfigService;
import com.devilvan.betrice.mapper.SysConfigMapper;
import com.devilvan.betrice.repository.SysConfigRepository;
import com.devilvan.betrice.util.UEmpty;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.devilvan.betrice.util.UCopy;

import java.util.List;

/**
* @author Evad.Wu
* @Description 全局配置信息表 业务逻辑类
* @date 2023-08-12
*/
@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfigPo> implements SysConfigService {
    private final SysConfigRepository sysConfigRepository;

    @Override
    public SysConfigPo saveUpdate(SysConfigDto sysConfigDto) {
        SysConfigPo sysConfigPo = new SysConfigPo();
        UCopy.fullCopy(sysConfigDto, sysConfigPo);
        return sysConfigRepository.saveAndFlush(sysConfigPo);
    }

    @Override
    public List<SysConfigPo> saveUpdateBatch(List<SysConfigDto> list) {
        List<SysConfigPo> sysConfigPoList = UCopy.fullCopyCollections(list, SysConfigPo.class);
        return sysConfigRepository.saveAllAndFlush(sysConfigPoList);
    }

    @Override
    public boolean selectCaptchaEnabled() {
        SysConfigPo sysConfigPo = sysConfigRepository.findByConfigKey(AdminConstant.SYS_ACCOUNT_CAPTCHA_ENABLED);
        String captchaEnabled = sysConfigPo.getConfigKey();
        if (UEmpty.isEmpty(captchaEnabled)) {
            return true;
        }
        return Convert.toBool(captchaEnabled);
    }
}
